package cn.edu.nuc.carsystem.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.nuc.carsystem.model.Car;
import cn.edu.nuc.carsystem.model.Custome;
import cn.edu.nuc.carsystem.model.ErrorHelp;
import cn.edu.nuc.carsystem.model.Home;
import cn.edu.nuc.carsystem.model.SysFunction;
import cn.edu.nuc.carsystem.model.SysUser;
import cn.edu.nuc.carsystem.service.interfaces.CustomeService;
import cn.edu.nuc.carsystem.service.interfaces.SysUserService;
@Controller
public class LoginController {

 @Autowired
  private CustomeService customeService;

 @Autowired
  private SysUserService sysUserService;
	
	   /**
	    * 注册新用户
	    * @param custome
	    * @return
	    */
	   @RequestMapping(value="/regist",method=RequestMethod.POST)
		public String regist(String name,String password,Integer sex){
		
		   Custome custome=new Custome();
		   custome.setCustomeidpassword(password);
		   custome.setCustomename(name);
		   custome.setCustomesex(new BigDecimal(sex));
		   Integer count=customeService.insert(custome);
		   return "redirect:/log";
	   }
	   /**
	    * 跳转注册页面
	    * @return
	    */
	   
	   @RequestMapping(value="/regis",method=RequestMethod.GET)
	  	public String regis(){
	  	   
	  	   return "custome/regist";
	     }
	   /**
	    * 注册校验
	    * @param name
	    * @return
	    */
	   @RequestMapping(value="/registCheck",method=RequestMethod.POST,produces = "application/text; charset=utf-8")
	   @ResponseBody
	  	public String regist(String username){
		   Custome cus=customeService.selectRegist(username);
		   System.out.println("cus"+cus);
		  if(cus!=null)
			  return "此用户已注册";
		  else
			  return "OK";
	   }

		 
		/**
		 * 
		 * 用户登录
		 * @param custome
		 * @param session
		 * @return
		 */
		 
		  @RequestMapping(value="/login",method=RequestMethod.POST)
			public ModelAndView login(Custome cust,HttpSession session){
			      ModelAndView model;
			      Object object=customeService.selectByName(cust);
			      if(object instanceof Custome){
				       Custome custome=(Custome)object;
				       session.setAttribute("custome", custome);
				       model =new ModelAndView("redirect:/");
				     
				       model.addObject("custome",custome);
				     
			      }
			      else{
				    	 
				    	  model =new ModelAndView("redirect:/log");
				    	  ErrorHelp error=(ErrorHelp)object;
				    	
				    	  model.addObject("error1",error.getName());
				    	  model.addObject("error2",error.getPassword());
			      }
			     
				  return model;
			    
				}
		  /**
		   * 跳转用户登录
		   * @return
		   */
		  @RequestMapping(value="/log",method=RequestMethod.GET)
			public String login(){
		      return "custome/customelogin";
			}
		  /**
		   * 职工登录
		   * @param sysUser
		   * @param session
		   * @return
		   */
		  @RequestMapping(value="/adminlogin",method=RequestMethod.POST)
				public ModelAndView login(SysUser sysUser,HttpSession session){
			     ModelAndView model;
			  
			      try {
					
				
			      List<SysFunction>  list=sysUserService.selectByName(sysUser);
			      model =new ModelAndView("sys/home");
			      session.setAttribute("sysUser", sysUser);
			     
			      model.addObject("sysUser",sysUser);
				  model.addObject("list",list);
			      System.out.println("session"+session.getAttribute("sysUser"));
			
				 // return model;
				  
			      } catch (Exception e) {
						// TODO: handle exception
			    	 
			    	  model =new ModelAndView("redirect:/admin");
			    	  model.addObject("message","输入有误");
				      
			      }
			     
			      return model;
				}
		  
		
	     

		  @RequestMapping(value="/admin",method=RequestMethod.GET)
			public String syslogin(){
		      return "sys/login";
			}
		  /**
		   * 首页
		   * @return
		   */
		  @RequestMapping(value="/",method=RequestMethod.GET)
			public String base(HttpSession session){
			  
		      return "base";
			}
	
}
